home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Tools / HexEdit 1.0.7 ƒ / HexEditSource / Source / UtilFuncs.c < prev   
Text File  |  1993-12-11  |  3KB  |  182 lines

  1. /*********************************************************************
  2.  * UtilFuncs.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *********************************************************************/
  7.  
  8. #include "HexEdit.h"
  9.  
  10. void SetControl(DialogPtr dial, int item, int value)
  11. {
  12.     Handle handle;
  13.     short type;
  14.     Rect r;
  15.  
  16.     GetDItem(dial,item,&type,&handle,&r);
  17.     SetCtlValue((ControlHandle) handle,value);
  18. }
  19.  
  20. // Copy a pascal string to a dialog's text item (static text or edit text)
  21. //
  22. void SetText(DialogPtr dial, int item, StringPtr text)
  23. {
  24.     Handle    handle;
  25.     short    type;
  26.     Rect    r;
  27.  
  28.     GetDItem(dial,item,&type,&handle,&r);
  29.     SetIText(handle,text);
  30. }
  31.  
  32. // Copy a dialog's text item to a pascal string.
  33. //
  34. void GetText(DialogPtr dial, int item, StringPtr text)
  35. {
  36.     Handle    handle;
  37.     short    type;
  38.     Rect    r;
  39.  
  40.     GetDItem(dial,item,&type,&handle,&r);
  41.     GetIText(handle,(StringPtr) text);
  42. }
  43.  
  44. void MyOutlineButton(DialogPtr dp, int bid, Pattern pat)
  45. {
  46.     Handle    h;
  47.     short    t;
  48.     Rect    r;
  49.     GrafPtr    gp;
  50.  
  51.     GetPort(&gp);
  52.     SetPort(dp);
  53.     GetDItem(dp,bid,&t,&h,&r);
  54.     PenSize(3,3);
  55.     PenPat(pat);
  56.     InsetRect(&r,-4,-4);
  57.     FrameRoundRect(&r,16,16);
  58.     PenNormal();
  59.     SetPort(gp);
  60. }
  61.  
  62. // Gray out a disabled button
  63. //
  64. void MyDisableButton(DialogPtr dp, int bid)
  65. {
  66.     Handle    h;
  67.     short    t;
  68.     Rect    r;
  69.     GrafPtr    gp;
  70.     
  71.     MyOutlineButton(dp,bid,white);
  72.     GetPort(&gp);
  73.     SetPort(dp);
  74.     GetDItem(dp,bid,&t,&h,&r);
  75.     HiliteControl((ControlHandle) h, 255);
  76.     PenPat(gray);
  77.     PenMode(patBic);
  78.     PaintRect(&r);
  79.     PenNormal();
  80.     SetPort(gp);
  81. }
  82.  
  83. // Restore a previously disabled button
  84. //
  85. void MyEnableButton(DialogPtr dp, int bid)
  86. {
  87.     Handle    h;
  88.     short    t;
  89.     Rect    r;
  90.     GrafPtr    gp;
  91.     
  92.     GetPort(&gp);
  93.     SetPort(dp);
  94.     GetDItem(dp,bid,&t,&h,&r);
  95.     HiliteControl((ControlHandle) h,0);
  96.     InvalRect(&r);
  97.     SetPort(gp);
  98. }
  99.  
  100. // Simulate the user pressing a button.  This is used to give the user some
  101. // visual feedback when they use the keyboard as a shortcut to press a dialog button.
  102. //
  103. void MySimulateButtonPress(DialogPtr dp, int bid)
  104. {
  105.     Handle    h;
  106.     short    t;
  107.     Rect    r;
  108.     GrafPtr    gp;
  109.     long    dmy;
  110.  
  111.     GetPort(&gp);
  112.     SetPort(dp);
  113.     GetDItem(dp,bid,&t,&h,&r);
  114.     InvertRoundRect(&r,4,4);
  115.     Delay(10,&dmy);
  116.     InvertRoundRect(&r,4,4);
  117.     Delay(10,&dmy);
  118.     SetPort(gp);
  119. }
  120.  
  121. short ErrorAlert(short severity, char *str,...)
  122. {
  123.     char     tbuf[128];
  124.     short    itemHit;
  125.     va_list args;
  126.  
  127.     va_start(args,str);
  128.     vsprintf(tbuf,str,args);
  129.     va_end(args);
  130.     CtoPstr(tbuf);
  131.     ParamText((StringPtr) tbuf,"\p","\p","\p");
  132.     InitCursor();
  133.     switch (severity) {
  134.     case ES_Message:
  135.         itemHit = Alert(StdMessageALRT,NULL);
  136.         break;
  137.     case ES_Note:
  138.         itemHit = NoteAlert(StdErrorALRT,NULL);
  139.         break;
  140.     case ES_Caution:
  141.         itemHit = CautionAlert(StdErrorALRT,NULL);
  142.         break;
  143.     case ES_Stop:
  144.         itemHit = StopAlert(StdErrorALRT,NULL);
  145.         break;
  146.     }
  147.  
  148.     if (itemHit == 2)
  149.         DebugStr((StringPtr) tbuf);
  150.  
  151.     if (severity == ES_Stop)
  152.         ExitToShell();
  153.  
  154.     return itemHit;
  155. }
  156.  
  157. short OSErrorAlert(short severity, char *str, short oe)
  158. {
  159.     return ErrorAlert(severity, "OS Error in %s: %d", str, oe);
  160. }
  161.  
  162. short MyRandom(short limit)
  163. {
  164.     unsigned long r;
  165.     r = (unsigned) Random();
  166.     r = (r*(long)limit)/65536L;
  167.     return (short) r;
  168. }
  169.  
  170. void MySetCursor(short n)
  171. {
  172.     extern Cursor gWatchCursor,gIBeamCursor;
  173.     static short lastN=-1;
  174.     if (n != lastN) {
  175.         lastN = n;
  176.         switch (n) {
  177.         case C_Arrow:    SetCursor(&arrow);            break;
  178.         case C_Watch:    SetCursor(&gWatchCursor);    break;
  179.         case C_IBeam:    SetCursor(&gIBeamCursor);    break;
  180.         }
  181.     }
  182. }